package com.deruan.hotcore.resource.model;

import java.util.Date;
import java.util.Iterator;

import javax.swing.ImageIcon;

import org.columba.addressbook.parser.ParserUtil;
import org.columba.addressbook.parser.SyntaxException;
import org.columba.addressbook.parser.XMLContactDocumentParser;
import org.jdom.Document;


import com.deruan.hotcore.base.common.bean.ResourceBean;
import com.deruan.hotcore.contact.model.AddressModel;
import com.deruan.hotcore.contact.model.ContactModel;
import com.deruan.hotcore.contact.model.IContactModel;
import com.deruan.hotcore.contact.model.IEmailModel;
import com.deruan.hotcore.contact.model.InstantMessagingModel;
import com.deruan.hotcore.contact.model.PhoneModel;
import com.deruan.hotcore.contact.model.VCARD;


public class ResourceModelXMLFactory 
{
	public static Document marshall(IResourceModel resourceModel)
	throws SyntaxException 
	{
        if (resourceModel == null)
	      throw new IllegalArgumentException("resourceModel == null");
        XMLContactDocumentParser parser = new XMLContactDocumentParser();
        parser.setId(resourceModel.getModelId());
        parser.set("sortstring", resourceModel.getSortString());
        Iterator iterator = resourceModel.getResourceIterator();
        while(iterator.hasNext())
        {
        	ResourceBean res = (ResourceBean)iterator.next();
        	parser.addResource(res);
        }
        return parser.getDocument();
}

public static ResourceModel unmarshall(Document document)
	throws SyntaxException 
	{
	
        if (document == null)
	        throw new IllegalArgumentException("document == null");
        XMLContactDocumentParser parser = new XMLContactDocumentParser(document);
        ResourceModel model = new ResourceModel(parser.get("id"));
        model.setSortString((parser.get("sortstring")));
        Iterator it = parser.getResourceIterator();
        while (it.hasNext()) 
        {
	        model.addResource((ResourceBean) it.next());
        }
        return model;
    }
}
